package pailaidai.hc.com.mysvgdemo.meetview;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.zhy.autolayout.AutoLinearLayout;

import pailaidai.hc.com.mysvgdemo.R;
import pailaidai.hc.com.mysvgdemo.activity.MeetFaceActivity;

/**
 * Created by Administrator on 2018/4/24.
 */

public class MeetView extends AutoLinearLayout implements View.OnClickListener {
    private ImageView itemmeet_image, itemmeet_paly;
    private AutoLinearLayout itemmeet_ly, itemmeet_edit;
    private TextView itemmeet_conttext;
    private String url;
    private boolean isedit;

    public interface MeetViewListener {
        void imageOnClickListener();

        void addLyOnClickListener();

        void editOnClickListener();
    }

    private MeetViewListener meetViewListener;

    public MeetViewListener getMeetViewListener() {
        return meetViewListener;
    }

    public void setMeetViewListener(MeetViewListener meetViewListener) {
        this.meetViewListener = meetViewListener;
    }

    public MeetView(Context context) {
        this(context, null);
    }

    public MeetView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MeetView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.meet_layout, this);
        TypedArray typedArray=context.obtainStyledAttributes(attrs,R.styleable.MeetView);


        initView(typedArray);
        ininListener();
    }

    private void ininListener() {
        itemmeet_image.setOnClickListener(this);
        itemmeet_ly.setOnClickListener(this);
        itemmeet_edit.setOnClickListener(this);
    }

    private void initView(TypedArray typedArray) {
        String context=typedArray.getString(R.styleable.MeetView_meettext);
        isedit=typedArray.getBoolean(R.styleable.MeetView_isEdit,false);
        itemmeet_image = findViewById(R.id.itemmeet_image);
        itemmeet_ly = findViewById(R.id.itemmeet_ly);
        itemmeet_conttext=findViewById(R.id.itemmeet_conttext);
        itemmeet_edit = findViewById(R.id.itemmeet_edit);
        itemmeet_paly = findViewById(R.id.itemmeet_paly);
        itemmeet_conttext.setText(context);
    }

    public void setUrl(String url) {
        this.url = url;
        Glide.with(getContext().getApplicationContext())
                .load(url)
                .transform(new CenterCrop(getContext().getApplicationContext()), new GlideRoundTransform(getContext().getApplicationContext()))
//                .bitmapTransform(new GlideRoundTransform(getContext().getApplicationContext()))
                .into(itemmeet_image);
        itemmeet_ly.setVisibility(GONE);
        if(isedit){
            itemmeet_edit.setVisibility(VISIBLE);
        }
        itemmeet_paly.setVisibility(VISIBLE);
        invalidate();
    }

    public void Restore() {
        Glide.with(getContext().getApplicationContext())
                .load("")
//                .bitmapTransform(new CropCircleTransformation(InformationActivity.this))
                .into(itemmeet_image);
        itemmeet_ly.setVisibility(VISIBLE);
        itemmeet_edit.setVisibility(GONE);
        itemmeet_paly.setVisibility(GONE);
        invalidate();
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.itemmeet_image:
                if (meetViewListener != null) {
                    meetViewListener.imageOnClickListener();
                }
                break;
            case R.id.itemmeet_ly:
                if (meetViewListener != null) {
                    meetViewListener.addLyOnClickListener();
                }
                break;
            case R.id.itemmeet_edit:
                if (meetViewListener != null) {
                    meetViewListener.editOnClickListener();
                }
                break;
        }

    }
}
